// 声明一个请求类
class Request {
  constructor(options = {}) {
    // 请求的根路径
    this.baseUrl = options.baseUrl || ''
    // 请求的 url 地址
    this.url = options.url || ''
    // 请求方式
    this.method = 'GET'
    // 请求的参数对象
    this.data = null
    // header 请求头
    this.header = options.header || {}
    this.beforeRequest = null
    this.afterRequest = null
  }

  get(url, data = {}) {
    this.method = 'GET'
    this.url = this.baseUrl + url
    this.data = data
    return this._()
  }

  post(url, data = {}) {
    this.method = 'POST'
    this.url = this.baseUrl + url
    this.data = data
    return this._()
  }

  put(url, data = {}) {
    this.method = 'PUT'
    this.url = this.baseUrl + url
    this.data = data
    return this._()
  }

  delete(url, data = {}) {
    this.method = 'DELETE'
    this.url = this.baseUrl + url
    this.data = data
    return this._()
  }

  _() {
    // 清空 header 对象
    this.header = {}
    // 请求之前做一些事
    this.beforeRequest && typeof this.beforeRequest === 'function' && this.beforeRequest(this)
    // 发起请求
    return new Promise((resolve, reject) => {
      let weixin = wx
      // 适配 uniapp
      if ('undefined' !== typeof uni) {
        weixin = uni
      }
      weixin.request({
        url: this.url,
        method: this.method,
        data: this.data,
        header: this.header,
        success: (res) => {
          resolve(res)
        },
        fail: (err) => {  
          reject(err)
        },
        complete: (res) => {
          // 请求完成以后做一些事情
          this.afterRequest && typeof this.afterRequest === 'function' && this
            .afterRequest(res)
        }
      })
    })
  }
}
// new出一个请求实例
const $http = new Request()
// const BASE = '10.2.5.9:8001';
// #ifdef MP-WEIXIN  
const BASE = 'http://localhost:3006';
// const BASE = 'http://62.234.5.237:8001';
export const WS_URL = `wss://localhost:3006/ws/`;
// export const WS_URL = 'wss://43.140.203.60:8001/ws/';
// #endif

// #ifdef H5
const BASE = '/api';
export const WS_URL = 'wws://localhost:3006/ws/';
// export const WS_URL = 'ws://62.234.5.237:8001/ws/';
// #endif

$http.baseUrl = `${BASE}`

export const HOST_URL = $http.baseUrl;
// 请求开始之前做一些事情
$http.beforeRequest = function(options) {
  console.log(options);
  try {
    let user = uni.getStorageSync("userObj");
    console.log(user);
    if (user) {
      const token = JSON.parse(user).token;
      options.header = {
        'token': token,
      }
    }
  } catch (e) {
    //TODO handle the exception
  }


}
// 请求完成之后做一些事情
$http.afterRequest = function() {
  uni.hideLoading()
}
// 判断请求类型进行请求
async function request(obj) {
  let res;
  if (obj.method == "get") {
    res = await $http.get(obj.url, obj.data)
  } else if (obj.method == "post") {
    res = await $http.post(obj.url, obj.data)
  } else if (obj.method == "put") {
    res = await $http.put(obj.url, obj.data)
  } else if (obj.method == "delete") {
    res = await $http.delete(obj.url, obj.data)
  }
  // 107 - 无效token
  // 105 - token过期
  if ([107, 106, 105].includes(res.data.code)) {
    uni.hideTabBar();
    uni.clearStorageSync();
    uni.redirectTo({
      url: "/pages/my/my"
    })
    return res;
  }
  return res;
}
// 进行导出这个请求方法
export default request